﻿using System;
using System.Configuration;
using System.Web.Configuration;

using Sunyi.Project.WebPoint.BusinessLogic;
using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataContract;

namespace ActivitySystem.Web
{
    public class Global : System.Web.HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            try
            {
                // TODO - Any better way to trust certificate?
                System.Net.ServicePointManager.ServerCertificateValidationCallback = ((source, certificate, chain, sslPolicyErrors) => true);

                // Code that runs on application startup
                EnvironmentSetting.WebSiteRootPath = Server.MapPath("~/");

                PageListMaintainer pageListMaintainer = new PageListMaintainer();
                pageListMaintainer.Run();

                #region Handle Development Account

                if (EnvironmentSetting.IsDebugMode)
                {
                    UserManager userManager = new UserManager();
                    User developmentAccount = userManager.SelectByName(WebPointGlobalConstant.DeveloperAccount);

                    if (developmentAccount != null && developmentAccount.IsActive == false)
                    {
                        developmentAccount.IsActive = true;

                        try
                        {
                            userManager.Update(developmentAccount);
                        }
                        catch { }
                    }
                }

                #endregion
            }
            catch { }
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started

        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.

        }
    }
}
